﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Dynamic;
using System.Linq;
using Newtonsoft.Json;
using Dynamic.Json;

public static class StringExtensions{
    public static string Clean( string input)
    {        
        string output = input.ToString().Replace("&quot;","");
        output = output.Replace("%22","");
        output = output.Replace("\"","");
        return output;
    }
}
/// <summary>
/// Summary description for ClassName
/// </summary>
public class YahooPipesDownloader
{
    public static IEnumerable<dynamic> GetPipe(string pipeId){
        var url ="http://pipes.yahoo.com/pipes/pipe.run?_id=" + pipeId + "&_render=json";
        var webClient = new System.Net.WebClient();
        var json = webClient.DownloadString(url);
        
         dynamic feed = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
        return ((IEnumerable<dynamic>)feed["value"]["items"]).Select(val => {
            dynamic d = new ExpandoObject();
                d.title= StringExtensions.Clean( val["title"].ToString());
                d.date = val["pubDate"];
                //if(val["source"]!=null)                                                            
                    d.url = StringExtensions.Clean( val["link"].ToString());
            return d;
            }
        ).ToArray();
    }
}

